package org.friut.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Time utils
 * 
 * @author Tang
 */
public class TimeUtils {

	private static final String DAFAULT_MONTH_FORMAT = "yyyy-MM";
	private static final String DAFAULT_DATE_FORMAT = "yyyy-MM-dd";
	private static final String DAFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final SimpleDateFormat monthFormat = new SimpleDateFormat(DAFAULT_MONTH_FORMAT);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(DAFAULT_DATE_FORMAT);
	private static final SimpleDateFormat timeFormat = new SimpleDateFormat(DAFAULT_TIME_FORMAT);

	/**
	 * Get day gap
	 * 
	 * @param now
	 * @param last
	 * @return
	 */
	public static int getDayGap(long time1, long time2) {
		long zeroTime1 = getZeroTime(time1);
		long zeroTime2 = getZeroTime(time2);
		int day = (int) ((zeroTime1 - zeroTime2) / 86400000L);
		return Math.abs(day);
	}

	/**
	 * Get zero time
	 * 
	 * @param time
	 * @return
	 */
	public static long getZeroTime(long time) {
		String str = dateFormat.format(new Date(time));

		long zeroTime = time;
		try {
			Date date = dateFormat.parse(str);
			zeroTime = date.getTime();
		} catch (ParseException e) {
		}

		return zeroTime;
	}

	/**
	 * Format time, yyyy-MM-dd HH:mm:ss
	 * 
	 * @param time
	 * @return
	 */
	public static String formatTime(long time) {
		String str = timeFormat.format(new Date(time));
		return str;
	}

	/**
	 * Format date, yyyy-MM-dd
	 * 
	 * @param time
	 * @return
	 */
	public static String formatDate(long time) {
		String str = dateFormat.format(new Date(time));
		return str;
	}
	
	/**
	 * Format month, yyyy-MM
	 * 
	 * @param time
	 * @return
	 * @throws ParseException 
	 */
	public static String formatMonth(String day) throws ParseException {
		Date date = dateFormat.parse(day);
		return monthFormat.format(date.getTime());
	}
	
	/**
	 * 获取指定时间的当月凌晨点
	 * 
	 * @return
	 */
	public static long getMonthZeroTime(long time){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		String str = format.format(new Date(time));

		long zeroTime = time;
		try {
			Date date = format.parse(str);
			zeroTime = date.getTime();
		} catch (ParseException e) {}
		return zeroTime;
	}
	
	/**
	 * 获取指定时间的当月凌晨点
	 * 
	 * @return
	 */
	public static long getMonthZeroTime(){
		long time = System.currentTimeMillis();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		String str = format.format(new Date(time));

		long zeroTime = time;
		try {
			Date date = format.parse(str);
			zeroTime = date.getTime();
		} catch (ParseException e) {}
		return zeroTime;
	}
	
	/**
	 * 获取当月结束的凌晨点
	 * 
	 * @return
	 */
	public static long getMonthEndZeroTime(long time){
		long startTime = TimeUtils.getMonthZeroTime(time);
		long endTime = startTime + TimeUtils.getMonthDays() * 86400000L;
		return endTime;
	}
	
	/**
	 * 获取当月天数
	 * 
	 * @return
	 */
	public static int getMonthDays(){
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		int day = aCalendar.getActualMaximum(Calendar.DATE);
		return day;
	}
	
	/**
	 * 获取当月天数
	 * 
	 * @return
	 */
	public static int getMonthDays(long time){
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		aCalendar.setTimeInMillis(time);
		int day = aCalendar.getActualMaximum(Calendar.DATE);
		return day;
	}
	
	/**
	 * 获取月份列表
	 * 
	 * @param startDay
	 * @param endDay
	 * @return
	 * @throws ParseException 
	 */
	public static Map<String, Long> getMonthMap(String startMonth, String endMonth) throws ParseException {
		Map<String, Long> monthMap = new HashMap<String, Long>();
		
		Date month1 = monthFormat.parse(startMonth);
		Date month2 = monthFormat.parse(endMonth);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(month1);
		while(calendar.getTime().before(month2)){
			monthMap.put(monthFormat.format(calendar.getTime()), calendar.getTime().getTime());
			calendar.add(Calendar.MONTH, 1);
		}
		return monthMap;
	}
}
